package worker.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import worker.model.service.WorkOrderService;
import worker.model.vo.HistoryVO;
import worker.model.vo.ItemInfoVO;
import worker.model.vo.ItemVO;
import worker.model.vo.ListVO;
import worker.model.vo.MemberVO;
import worker.model.vo.WorkOrdersVO;

public class WorkOrderController extends MultiActionController {
	private WorkOrderService workOrderService;

	public void setWorkOrderService(WorkOrderService workOrderService) {
		this.workOrderService = workOrderService;
	}
	/*
	 * Work Order main화면
	 */
	public ModelAndView main(HttpServletRequest request, HttpServletResponse response){
		System.out.println("WorkOrderController...main()");
		Map map = new HashMap();
		ListVO lvo=null;
		// work order 리스트
		String page = request.getParameter("page");
		if(page==null||page=="")
			page="1";
		lvo= this.workOrderService.getAllWorkOrderList(page);
		if (request.getParameter("ajax") != null) {
			return new ModelAndView("JsonView", "list", lvo);
		}
		return new ModelAndView("workorder.workorder_main","list",lvo);
	}
	/*
	 * WorkOrder.. Next Highest버튼을 클릭한다.
	 * WorkOrderId를 생성하여 보내준다. 
	 * WorkOrderId 생성 규칙
	 * 숫자_오늘날짜 
	 */
	public ModelAndView getWorkOrderIdandLastSerialNo(HttpServletRequest request, HttpServletResponse response){
		System.out.println("WorkOrderController...getWorkOrderIdandLastSerialNo()");
		HashMap map = this.workOrderService.getWorkOrderIdandLastSerialNo();
		return new ModelAndView("JsonView","wo_map",map);
	}
	/*
	 * 새로운 Work Order의 시작 시리얼 번호
	 */
	public ModelAndView getStartandEndSerialNo(HttpServletRequest request, HttpServletResponse response){
		System.out.println("WorkOrderController...getStartandEndSerialNo()");
		int quentity=Integer.parseInt(request.getParameter("quentity"));
		HashMap map= this.workOrderService.getStartandEndSerialNo(quentity);
		System.out.println("return WorkOrderController...getStartSerialNo()");
		System.out.println(map.toString());
		return new ModelAndView("JsonView","sno_map",map);
	}
	// WORKORDER 내용 저장
	public ModelAndView setWorkOrderInfo(HttpServletRequest request, HttpServletResponse response, WorkOrdersVO woVO){
		System.out.println("WorkOrderController...setWorkOrderInfo()");
		String flag= request.getParameter("flag");
		System.out.println("flag;;;;;"+flag);
		// 로그인 사용자 정보
		MemberVO memVO= (MemberVO) request.getSession().getAttribute("vo");
		String sno_start=Integer.toString(Integer.parseInt(woVO.getSerial_no_min()));
		woVO.setSerial_no_start(sno_start);
		System.out.println(woVO.toString());
		
		this.workOrderService.setWorkOrderInfo(woVO);
		HistoryVO hisVO = new HistoryVO(memVO,woVO);
		this.workOrderService.setHistorybyWorkOrderInfo(hisVO);
		
		Map map = new HashMap();
		ListVO lvo=null;
		// work order 리스트
		String page = request.getParameter("page");
		System.out.println(page);
		if(page==null||page=="")
			page="1";
		lvo= this.workOrderService.getAllWorkOrderList(page);
		if (request.getParameter("ajax") != null) {
			return new ModelAndView("JsonView", "list", lvo);
		}

		return new ModelAndView("workorder.workorder_main","list",lvo);
	}
	
	// 주문 수량에 따른 재고현황 검사
	public ModelAndView checkInventorybyOrderQuantity(HttpServletRequest request, HttpServletResponse response){
		int quantity = Integer.parseInt(request.getParameter("quantity"));
		String msg="", flag="false";
		int ice= 3*quantity;
		int rb= 2*quantity;
		int tp= (int) Math.ceil(0.5*quantity);	// 소수점 이하 무조건 올림
		int pc=quantity*1;
		
		ItemVO ivo= new ItemVO(ice, rb, tp, pc);
		HashMap map1= new HashMap();
		HashMap map= new HashMap();
		
		ArrayList<ItemInfoVO> item_list = this.workOrderService.getItemList();
		for(ItemInfoVO iivo:item_list){
			map.put(iivo.getItem_id(), iivo.getItem_amount());
		}
		//
		int i=(Integer) map.get("ICE");
		int r=(Integer)map.get("RB");
		int t= (Integer)map.get("TP");
		int p= (Integer)map.get("PC");
		if(ice>i || rb>r|| tp>t || pc>p){
			if(ice>i){
				msg="재고부족";
				flag="false";
				ivo.setItem_id_ice(ice);
			}
			if(rb>r){
				msg="재고부족";
				flag="false";
				ivo.setItem_id_rb(rb);
			}
			if(tp>t){
				msg="재고부족";
				flag="false";
				ivo.setItem_id_tp(tp);
			}
			if(pc>p){
				msg="재고부족";
				flag="false";
				ivo.setItem_id_pc(pc);
			}
		map1.put("msg",msg);
		map1.put("flag",flag);
		map1.put("ivo",ivo);
		}
		
		System.out.println(map1.toString());

		return new ModelAndView("JsonView","reMap",map1);
	}
}
